package com.hc.testaudio.deletea.test1

import android.media.AudioManager
import android.media.MediaPlayer
import com.hc.testaudio.App
import com.hc.testaudio.R
import com.yc.yclibx.comment.YcLog
import com.yc.yclibx.file.YcFileUtils
import java.io.File

/**
 * 音乐文件
 */
class MediaUtil : IMediaPlay {
    private var mMediaPlayer: MediaPlayer? = null
    private var mDataPath = YcFileUtils.SD_PATH + File.separator + "pink.mp3"
    private var mDataResource = R.raw.sin
    var mMediaListener: IMediaListener? = null
    @DataSourceType
    var mDataSourceType: Int = DataSourceType.RESOURCE
    /**
     * 是否需要重置播放的音乐（播放的音乐改变时为true）
     */
    private var mIsResetMedia: Boolean = false
    private var mIsLoop: Boolean = true //是否循环播放
    fun setData(dataSource: String, @DataSourceType dataSourceType: Int) {
        mIsResetMedia = if (dataSourceType != mDataSourceType) {
            true
        } else {
            mDataPath != dataSource
        }
        mDataPath = dataSource
        mDataSourceType = dataSourceType
    }

    fun setData(dataSource: Int) {
        mIsResetMedia = if (DataSourceType.RESOURCE != mDataSourceType) {
            true
        } else {
            mDataResource != dataSource
        }
        mDataResource = dataSource
        mDataSourceType = DataSourceType.RESOURCE
    }

    fun getAudioSession(): Int {
        return when (mMediaPlayer) {
            null -> 0
            else -> mMediaPlayer?.audioSessionId
        }!!
    }


    override fun pause() {
    }

    override fun stop() {

    }

    override fun play() {
        if (mMediaPlayer == null) {
            //创建和设置要播放的音频文件
            setDataSource()
            mMediaPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC) //音频流类型,音乐播放流
            mMediaPlayer!!.setOnErrorListener { mp: MediaPlayer, what: Int, extra: Int ->
                YcLog.e("播放错误：错误状态:$what  错误码：$extra")
                mMediaListener?.onErrorListener()
                false
            }
            mMediaPlayer!!.setOnPreparedListener { mp: MediaPlayer ->
                mp.start()
                mMediaListener?.onStartPlayingListener(mp)
            }
            mMediaPlayer!!.setOnCompletionListener {
                if (mIsLoop) {
                    it.start() //循环播放
                } else {
                    mMediaListener?.onCompletionListener()
                }
            }

            //当使用MediaPlayer.create(this, R.raw.xx)创建时会自动调用prepare()，
            if (mDataSourceType == DataSourceType.RESOURCE) {
                //                mMediaPlayer!!.start()
            } else {
                mMediaPlayer!!.prepareAsync()
            }

        } else {
            if (mIsResetMedia) {
                setDataSource()
                if (mDataSourceType == DataSourceType.RESOURCE) {
                    //                    mMediaPlayer!!.start()
                } else {
                    mMediaPlayer!!.prepareAsync()
                }
            } else {
                if (!mMediaPlayer!!.isPlaying) {
                    mMediaPlayer!!.start()
                } else {
                    mMediaPlayer!!.stop()
                }
            }

        }
    }

    /**
     * 设置播放音频路径/资源/地址
     */
    private fun setDataSource() {
        when (mDataSourceType) { //播放类型
            DataSourceType.NET -> {
                mMediaPlayer = MediaPlayer()
                mMediaPlayer?.setDataSource(mDataPath)
            }
            DataSourceType.SD -> {
                mMediaPlayer = MediaPlayer()
                mMediaPlayer?.setDataSource(mDataPath)
            }
            DataSourceType.RESOURCE -> {
                mMediaPlayer = MediaPlayer.create(App.mInstance, mDataResource)
            }
        }
    }

    override fun destroy() {
    }
}